package io.github.talelin.latticy.controller.v1.K;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.K.BGatewayDTO;
import io.github.talelin.latticy.model.K.BGatewayDO;
import io.github.talelin.latticy.service.K.BGatewayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
* @author generator@TaleLin
* @since 2021-06-03
*/
@RestController
@RequestMapping("/v1/Bgateway")
@Validated
public class BGatewayController {

    @Autowired
    private BGatewayService BGatewayService;

    @GetMapping("/{id}")
    public BGatewayDO getGateway(@PathVariable(value = "id")
                                     @Positive(message = "{id.positive}") Integer id) {
        BGatewayDO b_gateway = BGatewayService.getById(id);
        if (b_gateway == null) {
            throw new NotFoundException("b_gateway not found", 10022);
        }
        return b_gateway;
    }

    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated BGatewayDTO dto) {
        BGatewayDO BGatewayDO = new BGatewayDO();
        BeanUtils.copyProperties(dto,BGatewayDO);
        this.BGatewayService.save(BGatewayDO);
        return new CreatedVO();
    }


    @PutMapping("/{id}")
    @PermissionMeta("更新")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated BGatewayDTO dto,
                            @PathVariable @Positive(message = "{id.positive}") Long id) {
        BGatewayService.update(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable
                                @Positive(message = "{id.positive}") Long id) {
        BGatewayService.delete(id);
        return new DeletedVO();
    }

    @GetMapping("/page")
    public PageResponseVO<BGatewayDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<BGatewayDO> pager = new Page<>(page, count);
        IPage<BGatewayDO> paging = BGatewayService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<BGatewayDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword
     ) {
        Page<BGatewayDO> pager = new Page<>(page, count);
        IPage<BGatewayDO> paging = BGatewayService.seachBGateway(pager,keyword);
        return PageUtil.build(paging);
    }

}
